﻿namespace dotnet_notes.Extensions
{
    /// <summary>
    /// JSON 扩展类
    /// </summary>
    public static class JsonExtension
    {
        /// <summary>
        /// 强类型对象转 JSON
        /// </summary>
        public static string ToJson(this object? obj)
        {
            return obj != null ? JsonConvert.SerializeObject(obj) : "";
        }

        /// <summary>
        /// JSON 转强类型对象
        /// </summary>
        public static T ToObject<T>(this string? jsonString) where T : class
        { 
            if (jsonString.HasValue()) 
            {
                return JsonConvert.DeserializeObject<T>(jsonString);
            }

            return null;
        }
    }
}
